<?php

class AttachmentsController extends AppController {

	var $name = 'Attachments';
	var $uses = array('User', 'Attachment', 'Application');
	
	function beforeFilter() {
		parent::beforeFilter();
		if (isset($this->passedArgs[0]) && $this->Auth->user('group_id') == 2) {
			$model = ($this->params['action'] == 'add')? 'Application': 'Attachment';
  		$aco = $this->Acl->Aco->findByModelAndForeignKey($model, $this->passedArgs[0]);
    	$aro = $this->Acl->Aro->findByModelAndForeignKey('User', $this->Auth->user('id'));
    	if (!$this->Acl->check($aro['Aro'], $aco['Aco'])) {
      	$this->Session->setFlash($this->Auth->authError, 'flash_failure');
        $this->redirect($this->referer());
    	}
		}
	}
	
	/*
	*	add attachments.
	*/
	function add($app_id = null, $type = null) {
		//set view values, and type for saves. 
		$doc_type = null;
		
		if (!empty($this->data)) {
			$this->Attachment->create();
			if($this->Attachment->save($this->data)) {
				switch($this->Attachment->field('doc_type')) {	
					case "ps":
						$appStat = 'pstatement';
					break;
					case "ws":
						$appStat = 'wsample';
					break;
					case "cv":
						$appStat = 'depth';
					break;
				}
				if(isset($appStat)) $this->Application->change_status($app_id, $appStat, 2, '');
				$this->Session->setFlash("Document successfully added. ", 'flash_success', null, 'al');
			} else {
				$this->Session->setFlash(__('There was a problem saving the file attachment.', true), 
				'flash_failure', null, 'al');
			}
			
		} 
		
		switch($type) {	
			case "ps":
				$this->set('doc_type', "Personal Statement");
				$appStat = 'pstatement';
				$this->set("info", $this->_getContentMsg('attachments_personal_statement_info'));
			break;
			case "ws":
				$this->set('doc_type', "Writing Sample");
				$appStat = 'wsample';
				$this->set("info", $this->_getContentMsg('attachments_writing_sample_info'));
			break;
			case "cv":
				$this->set('doc_type', "Curriculum Vita");
				$appStat = 'depth';
				$this->set("info", $this->_getContentMsg('attachments_curriculum_vita_info'));
			break;
			default:
				$this->Session->setFlash(__('There was a problem accessing the page.', true), 
				'flash_failure');
			break;
		}
		
		$this->set('app_id', $app_id);
		$this->set('dt', $type);
		
		//get the records...
		$data = $this->Attachment->find('all', 
			array(
				'conditions' => array('Attachment.doc_type' => $type,
						      'Attachment.application_id' => $app_id),
				'fields' => array(
					'Attachment.id',
					'Attachment.modified', 
					'Attachment.file_name', 
					'Attachment.file_size'
				),
				'recursive' => 0
			)
		);
		
		$this->set(compact('data'));
	}
	
	/**
	*Delete an attachment.
	*/
	function delete($id=null) {
		
		if (!($id)) {
			$this->Session->setFlash(__('Invalid id for attachment', true), 'flash_failure');
			$this->redirect($this->referer());
		}
		
		$this->Attachment->id = $id;
		$app_id = $this->Attachment->field('application_id');
		$type = $this->Attachment->field('doc_type');
		$appStat = null;
		switch($type) {	
			case "ps":
				$this->set('doc_type', "Personal Statement");
				$appStat = 'pstatement';
			break;
			case "ws":
				$this->set('doc_type', "Writing Sample");
				$appStat = 'wsample';
			break;
			case "cv":
				$this->set('doc_type', "Curriculum Vita");
				$appStat = 'depth';
			break;
			default:
				$this->Session->setFlash(__('There was a problem accessing the page.', true), 
				'flash_failure');
			break;
		}
		
		if($this->Attachment->delete()) {
			if($this->Attachment->find('count', array('conditions' => array('Attachment.doc_type' => $type))) == 0)
				if(isset($appStat)) $this->Application->change_status($app_id, $appStat, 0, '');
			$this->Session->setFlash("Successfully deleted attachment.", 'flash_success', null, 'al');
		} else {
			$this->Session->setFlash(__('Error encountered, Unable to delete attachment.', true), 'flash_failure', null, 'al');
		}

		$this->set('app_id', $app_id);
		
		//get the records...
		$data = $this->Attachment->find('all', 
			array(
				'conditions' => array('Attachment.doc_type' => $type,
						      'Attachment.application_id' => $app_id),
				'fields' => array(
					'Attachment.modified', 
					'Attachment.file_name', 
					'Attachment.file_size'
				),
				'recursive' => 0
			)
		);
		$this->set(compact('data'));
		
		if ($this->RequestHandler->isAjax()) {
			$this->render('attachment_list', 'ajax');
		} else {
		  $this->redirect(array('action'=>'add', $app_id, $type));
		}
		
	}
	
	//Get a an attachment file.
	function view($id=null) {
		//do the find...This view sets headers and presents the content.
		if (!($id)) {
			$this->Session->setFlash(__('Invalid id for attachment', true), 'flash_failure');
			$this->redirect($this->referer());
		}
		
		$this->set('file', $this->Attachment->read(null, $id));
		$this->render('view', 'file');
	}
	
}

